"""
Created on 2011-06-02

@author: Olivier
"""

import types
import Core

class System(object):
    """
    classdocs
    """
    
    _genericString = "Generic System Name "
    
    _genericNameList = list()
    _name = ""
    _position = (0,0)
    _size = (1,1)
    _connectionList = list()
    
    def __init__(self, name = "", position = (0,0), size = (1,1)):
        """
        Constructor
        """
        #Create connection list
        self._connectionList = list()
        #Set name
        if not name:
            self._generateName()
        else:
            if type(name) is types.StringType:
                self._name = name
            else:
                raise ValueError("name must be a string but is " + str(type(name)))
        #Set position
        self.setPosition(position)
        #Set size
        self.setSize(size)
        
    def __del__(self):
        """
        """
        #delete generic names
        if self._genericString in self._name:
            self._name = self._name.replace(self._genericString, "")
            self._genericNameList.remove(int(self._name))
        
    def __str__(self):
        """
        """
        return self._name + " at position " + str(self._position)
            
    def _generateName(self):
        """
        """
        if len(self._genericNameList) == 0:
            self._name = self._genericString + "1"
            self._genericNameList.append(1)
        else:
            self._name = self._genericString + str(self._genericNameList[-1] + 1)
            self._genericNameList.append(self._genericNameList[-1] + 1)
        #Sort list to have kind of an order
        self._genericNameList.sort()
        
    def getName(self):
        """
        """
        return self._name
    
    def setName(self, name):
        """
        """
        if type(name) is not types.StringType:
            raise ValueError("name must be a string but is " + str(type(name)))
        if self._genericString in self._name:
            self._name = self._name.replace(self._genericString, "")
            self._genericNameList.remove(int(self._name))
        self._name = name
        
    def setPosition(self, position):
        """
        """
        if type(position) is types.TupleType:
            if len(position) is 2:
                self._position = position
            else:
                raise ValueError("position must be a tuple of length 2 but is of length " + str(len(position)))
        else:
            raise ValueError("position must be a tuple but is " + str(type(position)))
        
    def getPosition(self):
        """
        """
        return self._position
    
    def setSize(self, size):
        """
        """
        if type(size) is types.TupleType:
            if len(size) is 2:
                if size[0] > 0 and size[1] > 0:
                    self._position = size
                else:
                    raise ValueError("size must be composed of positive values only. Trying to set " + str(size))
            else:
                raise ValueError("size must be a tuple of length 2 but is of length " + str(len(size)))
        else:
            raise ValueError("size must be a tuple but is " + str(type(size)))
        
    def getSize(self):
        """
        """
        return self._size
        
    def addConnection(self, name = "", position = (0,0), snap = False):
        """
        """
        #Find position
        finalPosition = position
        if snap:
            finalPosition = self._getNearestSnap(position)
        #addConnection
        self._connectionList.append(Core.Connection.Connection(name, Core.Connection.Connection.UNCONNECTED, finalPosition))
        
    def _validateConnectionPosition(self, position = (0,0)):
        """
        """
        if abs(position[0]) == (self.getSize()[0] / 2) and abs(position[1]) <= (self.getSize()[1] / 2):
            return True
        elif abs(position[1]) == (self.getSize()[1] / 2) and abs(position[0]) <= (self.getSize()[0] / 2):
            return True
        return False
    
    def _getNearestSnap(self, position = (0,0)):
        """
        """
        snapPosition = (0,0)
        if type(position) is types.TupleType:
            if len(position) is 2:
                #Find snap point
                if (float(position[0]) / float(self.getSize()[0])) > (float(position[1]) / float(self.getSize()[1])):
                    x = self.getSize()[0] / 2
                    y = position[1]
                    snapPosition = (x,y)
                else:
                    y = self.getSize()[1] / 2
                    x = position[0]
                    snapPosition = (x,y)
                return snapPosition                    
            else:
                raise ValueError("position must be a tuple of length 2 but is of length " + str(len(position)))
        else:
            raise ValueError("position must be a tuple but is " + str(type(position)))
        
    def getConnection(self, name = "", position = (-1,-1), id = -1):
        """
        """
        possibleConnections = list()
        for connection in self._connectionList:
            if connection.getName() == name:
                if position[0] != -1:
                    if connection.getPosition()[0] != position[0] or connection.getPosition()[1] != position[1]:
                        continue
                if id != -1:
                    if connection.getID() != id:
                        continue
                possibleConnections.append(connection)
        return possibleConnections[:]
    
    def connect(self, connection = Core.Connection.Connection(), link = Core.Link.Link()):
        """
        """
         
        
if __name__ == "__main__":
    print float(5)/float(6)
        